package com.schoolvisitors.controller.admin;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.CheckPermission;
import com.schoolvisitors.enums.PermissionEnum;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.AdminPermissionService;
import com.schoolvisitors.service.SystemConfigService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/admin")
public class SystemSettingController {

    private final SystemConfigService systemConfigService;
    private final AdminPermissionService adminPermissionService;

    public SystemSettingController(SystemConfigService systemConfigService, AdminPermissionService adminPermissionService) {
        this.systemConfigService = systemConfigService;
        this.adminPermissionService = adminPermissionService;
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getSystemConfig")
    public ResultBody getConfigs(){

        JSONObject data = new JSONObject();

        data.put("defaultPoint", systemConfigService.getSystemConfigToLong("defaultPoint"));
        data.put("notice", systemConfigService.getSystemConfigToString("notice"));
        data.put("swipeTop", JSON.parseArray(systemConfigService.getSystemConfigToString("swipeTop")));
        data.put("swipeBottom", JSON.parseArray(systemConfigService.getSystemConfigToString("swipeBottom")));
        data.put("advanceReservationDay", systemConfigService.getSystemConfigToLong("advanceReservationDay"));
        data.put("checkBeforeAfterTime", systemConfigService.getSystemConfigToLong("checkBeforeAfterTime"));


        return ResultBody.success(data);
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/updateSystemConfig")
    public ResultBody updateSystemConfig(@RequestBody JSONObject data){
        String key = data.getString("key");
        String value = data.getString("value");
        systemConfigService.updateSystemConfig(key, value);
        return ResultBody.success();
    }
}
